home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / DELPHI / TSMTP32.ZIP / MMAIL.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-06-29  |  6.4 KB  |  249 lines

  1. unit Mmail;
  2.  
  3. interface
  4.  
  5. uses
  6.   SysUtils, Windows, Messages, Classes, Graphics, Controls,
  7.   Forms, Dialogs, Buttons, ExtCtrls, StdCtrls, Smtp,
  8.   SMTPSu,IniFiles, ComCtrls, MailUtil, mailbase;
  9.  
  10. type
  11.   TMailForm = class(TForm)
  12.     ToolBar: TPanel;
  13.     SendButton: TSpeedButton;
  14.     SetupButton: TSpeedButton;
  15.     ExitButton: TSpeedButton;
  16.     SendInfoPanel: TPanel;
  17.     ToEdit: TEdit;
  18.     Label1: TLabel;
  19.     Label3: TLabel;
  20.     SubjectEdit: TEdit;
  21.     CancelButton: TSpeedButton;
  22.     Label2: TLabel;
  23.     BodyMemo: TMemo;
  24.     AttachButton: TSpeedButton;
  25.     AttachmentsComboBox: TComboBox;
  26.     Mailer: TSMTP;
  27.     Panel1: TPanel;
  28.     StatusBar1: TStatusBar;
  29.     ProgressBar1: TProgressBar;
  30.     procedure ExitButtonClick(Sender: TObject);
  31.     procedure SendButtonClick(Sender: TObject);
  32.     procedure CancelButtonClick(Sender: TObject);
  33.     procedure SetupButtonClick(Sender: TObject);
  34.     procedure FormCreate(Sender: TObject);
  35.     procedure FormClose(Sender: TObject; var Action: TCloseAction);
  36.     procedure AttachButtonClick(Sender: TObject);
  37.     procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  38.     procedure MailerProgress(Sender: TObject);
  39.     procedure MailerStatusChange(Sender: TObject);
  40.   private
  41.     { Private declarations }
  42.     IniName : string;
  43.     procedure EnableControls;
  44.     procedure DisableControls;
  45.   public
  46.     { Public declarations }
  47.   end;
  48.  
  49. var
  50.   MailForm: TMailForm;
  51.  
  52. implementation
  53.  
  54. {$R *.DFM}
  55.  
  56. procedure TMailForm.EnableControls;
  57. var
  58.   i : Integer;
  59.   SB : TSpeedButton;
  60. begin
  61.   for i:=0 to ToolBar.ControlCount-1 do
  62.   if ToolBar.Controls[i] is TSpeedButton then
  63.   begin
  64.     SB:=ToolBar.Controls[i] as TSpeedButton;
  65.     SB.Enabled:=SB.Tag<>1;
  66.   end;
  67.   SendInfoPanel.Enabled:=true;
  68.   ProgressBar1.Visible:=false;
  69.   BodyMemo.Enabled:=true;
  70.   Cursor:=crDefault;
  71. end;
  72.  
  73. procedure TMailForm.DisableControls;
  74. var
  75.   i : Integer;
  76.   SB : TSpeedButton;
  77. begin
  78.   for i:=0 to ToolBar.ControlCount-1 do
  79.   if ToolBar.Controls[i] is TSpeedButton then
  80.   begin
  81.     SB:=ToolBar.Controls[i] as TSpeedButton;
  82.     SB.Enabled:=SB.Tag=1;
  83.   end;
  84.   SendInfoPanel.Enabled:=false;
  85.   ProgressBar1.Visible:=true;
  86.   BodyMemo.Enabled:=false;
  87.   Cursor:=crHourGlass;
  88. end;
  89.  
  90. procedure TMailForm.ExitButtonClick(Sender: TObject);
  91. begin
  92.   Close;
  93. end;
  94.  
  95. procedure TMailForm.SendButtonClick(Sender: TObject);
  96. begin
  97.   if (Mailer.Server='') or (Mailer.From='') then
  98.     MessageDlg('You might want to enter the information'^M^J+
  99.                'in the Setup dialog box...',mtError,[mbOk],0)
  100.   else
  101.   if ToEdit.Text='' then
  102.     MessageDlg('You might want to enter the recipient''s address first...'
  103.                ,mtError,[mbOk],0)
  104.   else
  105.   with Mailer do
  106.   begin
  107.     Recipient:=ToEdit.Text;
  108.     Subject:=SubjectEdit.Text;
  109.     Body:=BodyMemo.Lines;
  110.     Attachments:=AttachmentsComboBox.Items;
  111.     DisableControls;
  112.     try
  113.       SendSingleMessage;
  114.     finally
  115.       EnableControls;
  116.     end;
  117.   end;
  118. end;
  119.  
  120. procedure TMailForm.CancelButtonClick(Sender: TObject);
  121. begin
  122.   Mailer.Cancel;
  123. end;
  124.  
  125. procedure TMailForm.SetupButtonClick(Sender: TObject);
  126. var
  127.   A,N : string;
  128. begin
  129.   with TSetupDlg.Create(Self) do
  130.   try
  131.     ServerEdit.Text:=Mailer.Server;
  132.     BreakLine(Mailer.From,A,N);
  133.     UserAddressEdit.Text:=A;
  134.     UserNameEdit.Text:=N;
  135.     LogFileNameEdit.Text:=Mailer.LogFileName;
  136.     EncodeComboBox.ItemIndex:=Ord(Mailer.Encoding);
  137.     CharSetComboBox.ItemIndex:=Ord(Mailer.CharSet);
  138.     ShowModal;
  139.     if ModalResult=mrOk then
  140.     begin
  141.       Mailer.Server:=ServerEdit.Text;
  142.       Mailer.From:=UserAddressEdit.Text;
  143.       Mailer.From:=JoinLines(UserAddressEdit.Text,UserNameEdit.Text);
  144.       Mailer.LogFileName:=LogFileNameEdit.Text;
  145.       Mailer.Encoding:=TEncoding(EncodeComboBox.ItemIndex);
  146.       Mailer.CharSet:=TCharSet(CharSetComboBox.ItemIndex);
  147.     end;
  148.   finally
  149.     free;
  150.   end;
  151. end;
  152.  
  153. procedure TMailForm.FormCreate(Sender: TObject);
  154. var
  155.   A,N : string;
  156.   i : Integer;
  157. begin
  158.   IniName:=ChangeFileExt(Application.ExeName,'.ini');
  159.   with TIniFile.Create(IniName) do
  160.   try
  161.     Mailer.Server:=ReadString('Setup','Server','');
  162.     A:=ReadString('Setup','EMail Address','');
  163.     N:=ReadString('Setup','Name','');
  164.     Mailer.From:=JoinLines(A,N);
  165.     Mailer.LogFileName:=ReadString('Setup','Log File','');
  166.     Mailer.Encoding:=TEncoding(ReadInteger('Setup','Encoding',0));
  167.     Mailer.CharSet:=TCharSet(ReadInteger('Setup','CharSet',0));
  168.   finally
  169.     free;
  170.   end;
  171.   for i:=1 to ParamCount do
  172.     AttachmentsComboBox.Items.Add(ParamStr(i));
  173.   AttachmentsComboBox.ItemIndex:=0;
  174. end;
  175.  
  176. procedure TMailForm.FormClose(Sender: TObject; var Action: TCloseAction);
  177. var
  178.   A,N : string;
  179. begin
  180.   with TIniFile.Create(IniName) do
  181.   try
  182.     WriteString('Setup','Server',Mailer.Server);
  183.     BreakLine(Mailer.From,A,N);
  184.     WriteString('Setup','EMail Address',A);
  185.     WriteString('Setup','Name',N);
  186.     WriteString('Setup','Log File',Mailer.LogFileName);
  187.     WriteInteger('Setup','Encoding',Ord(Mailer.Encoding));
  188.     WriteInteger('Setup','CharSet',Ord(Mailer.CharSet));
  189.   finally
  190.     free;
  191.   end;
  192. end;
  193.  
  194. procedure TMailForm.AttachButtonClick(Sender: TObject);
  195. begin
  196.   with TOpenDialog.Create(Self) do
  197.   try
  198.     Filter:='All Files (*.*)|*.*';
  199.     Options:=[ofHideReadOnly,ofAllowMultiSelect];
  200.     if Execute then
  201.     begin
  202.       AttachmentsComboBox.Items.Assign(Files);
  203.       AttachmentsComboBox.ItemIndex:=0;
  204.     end
  205.     else
  206.       AttachmentsComboBox.Items.Clear;
  207.   finally
  208.     Free;
  209.   end;
  210. end;
  211.  
  212. procedure TMailForm.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  213. begin
  214.   if not SendInfoPanel.Enabled then
  215.   begin
  216.     MessageDlg('Transfer in Progress.  Click Cancel button first.',
  217.                mtError,[mbOk],0);
  218.     CanClose:=false;
  219.   end
  220.   else
  221.     CanClose:=true;
  222. end;
  223.  
  224. procedure TMailForm.MailerProgress(Sender: TObject);
  225. begin
  226.   ProgressBar1.Position:=Mailer.Progress;
  227. end;
  228.  
  229. procedure TMailForm.MailerStatusChange(Sender : TObject);
  230. var
  231.   s : string;
  232. begin
  233.   case Mailer.Status of
  234.    msIdle : s:='';
  235.    msLogIn : s:='Logging In';
  236.    msLogOut : s:='Logging Out';
  237.    msHeaders : s:='Sending headers';
  238.    msEnvelope : s:='Sending commands';
  239.    msBody : s:='Sending message body';
  240.    msAttachment : s:='Sending attachment(s)';
  241.    msCancel : s:='Canceled';
  242.    msEnCode : s:='Encoding the attachment(s)';
  243.    msConnecting : s:='Connecting to Server';
  244.   end;
  245.   StatusBar1.Panels[0].Text:=s;
  246. end;
  247.  
  248. end.
  249.